package com.dimples.dd.auth.core.captcha.service.impl;

import com.dimples.dd.auth.core.captcha.properties.CaptchaProperties;
import com.dimples.dd.auth.core.captcha.service.ValidateCodeService;
import com.dimples.dd.common.constant.DDConstant;
import com.dimples.dd.common.constant.ImageTypeConstant;
import com.dimples.dd.common.constant.ParamsConstant;
import com.dimples.dd.web.exception.ValidateCodeException;
import com.dimples.dd.redis.core.RedisHelper;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static com.dimples.dd.common.result.ResultCode.*;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/6/18
 */
@Service
@RequiredArgsConstructor
public class ValidateCodeServiceImpl implements ValidateCodeService {

    private final RedisHelper redisService;
    private final CaptchaProperties properties;

    @Override
    public void create(HttpServletRequest request, HttpServletResponse response) throws ValidateCodeException, IOException {
        String key = request.getParameter(ParamsConstant.VALIDATE_CODE_KEY);
        if (StringUtils.isBlank(key)) {
            throw new ValidateCodeException("验证码key不能为空");
        }
        setHeader(response, properties.getType());

        Captcha captcha = createCaptcha(properties);
        redisService.strSetEx(DDConstant.CODE_PREFIX + key, StringUtils.lowerCase(captcha.text()), properties.getTime(), TimeUnit.SECONDS);
        captcha.out(response.getOutputStream());
    }

    @Override
    public void check(String key, String value) throws ValidateCodeException {
        Object codeInRedis = redisService.strGet(DDConstant.CODE_PREFIX + key);
        if (StringUtils.isBlank(value)) {
            throw new ValidateCodeException(VALIDATE_CODE_NOT_NULL);
        }
        if (codeInRedis == null) {
            throw new ValidateCodeException(VALIDATE_CODE_EXPIRED);
        }
        if (!StringUtils.equalsIgnoreCase(value, String.valueOf(codeInRedis))) {
            throw new ValidateCodeException(VALIDATE_CODE_ERROR);
        }
    }

    private Captcha createCaptcha(CaptchaProperties code) {
        Captcha captcha = null;
        if (StringUtils.equalsIgnoreCase(code.getType(), ImageTypeConstant.GIF)) {
            captcha = new GifCaptcha(code.getWidth(), code.getHeight(), code.getLength());
        } else {
            captcha = new SpecCaptcha(code.getWidth(), code.getHeight(), code.getLength());
        }
        captcha.setCharType(code.getCharType());
        return captcha;
    }

    private void setHeader(HttpServletResponse response, String type) {
        if (StringUtils.equalsIgnoreCase(type, ImageTypeConstant.GIF)) {
            response.setContentType(MediaType.IMAGE_GIF_VALUE);
        } else {
            response.setContentType(MediaType.IMAGE_PNG_VALUE);
        }
        response.setHeader(HttpHeaders.PRAGMA, "No-cache");
        response.setHeader(HttpHeaders.CACHE_CONTROL, "No-cache");
        response.setDateHeader(HttpHeaders.EXPIRES, 0L);
    }
}
